package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.RoleService;
import com.tgy.dTree.Dtree;
import com.tgy.page.PageQuery;
import com.tgy.pojo.SysRole;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/24 14:33
 */
@Validated
@Api(value = "后台管理角色控制器", tags = {"后台管理角色管理"})
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation("获取后台管理角色列表")
    @SaCheckPermission("sysrole:query")
    @GetMapping("getSysRoleList")
    public LayuiTableResult getSysRoleList(SysRole bo, PageQuery pageQuery){
        return roleService.getSysRoleList(bo,pageQuery);
    }

    @ApiOperation("修改后台管理角色状态")
    @SaCheckPermission("sysrole:update")
    @PostMapping("updateStatus")
    public GraceJSONResult updateStatus(SysRole sysRole){
        return roleService.updateStatus(sysRole.getId(),sysRole.getAvailable());
    }

    @ApiOperation("添加后台管理角色")
    @SaCheckPermission("sysrole:add")
    @PostMapping("addSysRole")
    public GraceJSONResult addSysRole(SysRole sysRole){
        return roleService.addSysRole(sysRole);
    }

    @ApiOperation("修改后台管理角色")
    @SaCheckPermission("sysrole:update")
    @PostMapping("updateSysRole")
    public GraceJSONResult updateSysRole(SysRole sysRole){
        return roleService.updateSysRole(sysRole);
    }

    @ApiOperation("删除后台管理角色")
    @SaCheckPermission("sysrole:del")
    @PostMapping("delSysRole")
    // @PostMapping("delSysRole/{id}")
    public GraceJSONResult delSysRole(@ApiParam("ID必须传入") String id){
        return roleService.delSysRole(id);
    }

    @ApiOperation("批量删除后台管理角色")
    @SaCheckPermission("sysrole:del")
    @PostMapping("delSysRoleIds")
    public GraceJSONResult delSysRoleIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return roleService.delSysRoleIds(list);
    }

    @ApiOperation("根据ID查询后台管理角色")
    @SaCheckPermission
    @PostMapping("getSysRoleById")
    public GraceJSONResult getSysRoleById(@ApiParam("ID必须传入") String id){
        return roleService.getSysRoleById(id);
    }

    @ApiOperation("获取所有权限，且回显选中角色的权限有哪些。此处返回格式不需要以children的格式返回")
    // @SaCheckPermission
    @GetMapping("getSysRolePermission")
    public Dtree getSysRolePermission(@ApiParam("角色ID必须传入") String rid){
        return roleService.getSysRolePermission(rid);
    }

    @ApiOperation("给角色添加新的权限")
    @SaCheckPermission("sysrole:power")
    @PostMapping("addSysRolePermission")
    public GraceJSONResult addSysRolePermission(@ApiParam("角色ID必须传入") String rid,String pids){
        return roleService.addSysRolePermission(rid,pids);
    }

}
